import 'package:boss/color_constant.dart';
import 'package:boss/main.dart';
import 'package:boss/models/pay_qr_code.dart';
import 'package:boss/provider/login_user_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/pay/payList/pay_list_page.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/custom_divier.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

@RouterConfig(PayBindPage.pageName)
class PayBindPage extends StatefulWidget {
  static const pageName = '/PayBindPage';

  @Autowired()
  final String qrCode;

  PayBindPage(this.qrCode, {Key key}) : super(key: key);

  @override
  _PayBindPageState createState() => _PayBindPageState();
}

class _PayBindPageState extends BaseState<PayBindPage> {
  _PayBindPageState() : super(PayBindPage.pageName);

  String storeName;

  String describe = '';

  @override
  void initState() {
    super.initState();

    storeName = Provider.of<LoginUserProvider>(context, listen: false).loginUser.storeName;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '绑定信息确认',
      ),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              padding: EdgeInsets.zero,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    height: 70.w,
                    color: ColorConstant.fefbe3,
                    padding: EdgeInsets.symmetric(horizontal: 38.w),
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '【$storeName】与二维码成为绑定关系后，通过二维码交易的金额将会结算到该 门店账户中',
                      style: TextStyleConstant().origin_20,
                    ),
                  ),
                  ShadowRadiusCard(
                    padding: EdgeInsets.all(30.w),
                    margin: EdgeInsets.only(left: 20.w, right: 20.w, top: 30.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('二维码编号', style: TextStyleConstant().normal_1_28),
                        Container(
                          child: Text('${widget.qrCode}', style: TextStyleConstant().normal_1_28),
                          margin: EdgeInsets.only(top: 25.w, bottom: 44.w),
                        ),
                        Text('备注信息', style: TextStyleConstant().normal_1_28),
                        Container(
                          height: 50.w,
                          margin: EdgeInsets.only(top: 25.w),
                          child: TextField(
                            style: TextStyleConstant().normal_1_28,
                            decoration: InputDecoration(
                              contentPadding: EdgeInsets.zero,
                              hintText: '文字数目不超过20字，例如：西区使用',
                              hintStyle: TextStyleConstant().normal_4_28,
                              isDense: true,
                              border: NoInputBorder(),
                              focusedErrorBorder: NoInputBorder(),
                              focusedBorder: NoInputBorder(),
                              errorBorder: NoInputBorder(),
                              enabledBorder: NoInputBorder(),
                              disabledBorder: NoInputBorder(),
                            ),
                            onChanged: (value) {
                              describe = value;
                            },
                          ),
                        ),
                        CustomDivider(),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          FixedBottomBtn(
            title: '确认绑定',
            onTap: bindQrCode,
          ),
        ],
      ),
    );
  }

  void bindQrCode() async {
    if (describe.length > 20) {
      toast('备注不能超过20个字');
      return;
    }

    try {
      showLoadingToast();
      final res = await api.bindPayQrCode(PayQrCode(qrcode: widget.qrCode, describe: describe));
      if (!res.successful()) {
        toast(res.msg);
        return;
      }
      toast('绑定成功');

      Navigator.popAndPushNamed(context, PayListPage.pageName);
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }
}
